/*
    该程序仅用于调试驱动电压的控制、放大链路的测试
*/

#include "MephistoCAM.h"

uint32_t get_drv_signame( char *drv_signame );
void print_opts();

int main( int argc, char *argv[] ){

#if defined(_DEBUG_CCD_DRV_)
    if( argc != 2 && argc != 3 ){
        printf("> usage: %s drv_signame \n", argv[0]);
        printf("> or\n");
        printf("> usage: %s drv_signame state(0/1)\n", argv[0]);
        print_opts();
        exit(0);
    }

    int state;
    if( argc == 3 ){
        state = atoi(argv[2]);
    }
    
    uint32_t BIT_POS = get_drv_signame(argv[1]);

    int fd = open("/dev/mem", O_RDWR | O_SYNC);
    uint *ptr1 = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, ADDR_GPIO_OUT);
    uint *gpio_reg      = ptr1;     // 0x41200000


    if( argc == 3 ){
        if( state == 0 ){
            clrbit( *gpio_reg, BIT_POS );
        }
        else if( state == 1 ){
            setbit( *gpio_reg, BIT_POS );
        }
        return 0;
    }

    if( argc == 2 ){
        while(1){
            clrbit( *gpio_reg, BIT_POS );
            usleep(1);
            setbit( *gpio_reg, BIT_POS );
            usleep(1);
        }
    }
#else
    printf("\n> ///////////////////////////////////////////////////////////////////////////////////////\n");
    printf("> This program(%s) should be used only for debugging CCD drive & OSx amplifier routes\n", argv[0]);
    printf("> ///////////////////////////////////////////////////////////////////////////////////////\n\n");
#endif

    munmap(ptr1,10);
    close(fd);

    return 0;
}


uint32_t get_drv_signame( char *drv_signame ){

    // char drv_signame[32];
    // sprintf(drv_signame, "%s", drv_signame_xxx);

    printf("> (debug_ccd_drv) drv_signame = %s\n", drv_signame);
    uint32_t opt = 0;
  
#if defined(_DEBUG_CCD_DRV_)
  
    if( strcmp(drv_signame, "imgclk_ad1") == 0 )
    {
        opt = BIT_IMGCLK_AD1_TGA;
        printf("> debuging IMGCLK_AD1_TGA ...\n");
    }
    else if( strcmp(drv_signame, "imgclk_ad2") == 0 )
    {
        opt = BIT_IMGCLK_AD2;
        printf("> debuging IMGCLK_AD2 ...\n");
    } 
    else if( strcmp(drv_signame, "imgclk_ad3") == 0 )
    {
        opt = BIT_IMGCLK_AD3;
        printf("> debuging IMGCLK_AD3 ...\n");
    } 
    else if( strcmp(drv_signame, "imgclk_ad4") == 0 )
    {
        opt = BIT_IMGCLK_AD4_TGD;
        printf("> debuging IMGCLK_AD4_TGA ...\n");
    } 
    else if( strcmp(drv_signame, "bias_ctr") == 0 )
    {
        opt = BIT_BIAS_CTR;
        printf("> debuging BIAS CTR ...\n");
    } 
    else if( strcmp(drv_signame, "phi1") == 0 )
    {
        opt = BIT_PHI1;
        printf("> debuging Phi1 ...\n");
    } 
    else if( strcmp(drv_signame, "phi2") == 0 )
    {
        opt = BIT_PHI2;
        printf("> debuging Phi2 ...\n");
    } 
    else if( strcmp(drv_signame, "phi3") == 0 )
    {
        opt = BIT_PHI3;
        printf("> debuging Phi3 ...\n");
    } 
    else if( strcmp(drv_signame, "rg_ad") == 0 )
    {
        opt = BIT_RG_AD;
        printf("> debuging Reset Gate A/D ...\n");
    } 
    else if( strcmp(drv_signame, "10v") == 0 )
    {
        opt = BIT_10V_CTL;
        printf("> debuging Reset 10V_CTL ...\n");
    }
    else 
    {
        print_opts();
        exit(0);
    }

#endif
    return opt;
}

void print_opts(){
    printf("> *drv_signame MUST be one of the following:\n");
    printf("> imgclk_ad1\n");
    printf("> imgclk_ad2\n");
    printf("> imgclk_ad3\n");
    printf("> imgclk_ad4\n");
    printf("> bias_ctr\n");
    printf("> phi1\n");
    printf("> phi2\n");
    printf("> phi3\n");
    printf("> sw\n");
    printf("> rg_ad\n");
    printf("> 10V\n");
}